package com.dxyxx.db;

import android.content.ContentResolver;
import android.content.Context;
import android.database.Cursor;
import android.net.Uri;

public class DataQueryUtils {

	// 根据 id 逆序查询。。 最新的，肯定是最晚入库的，id最大。
	// 写一个 函数，能够分页 查询 limit 8 offset xx 跳过几行限制几行
	public static Cursor getCursor(Context context, String id, String count) {
		ContentResolver conre = context.getContentResolver();
		return conre.query(DataChangeProvider.CONTENT_URI, new String[] { "id", "pid", "description" }, "id<?", new String[] { id }, " id DESC LIMIT " + count); // 降序查询
	}

	// 根据 id 逆序查询。。 最新的，肯定是最晚入库的，id最大。
	// 写一个 函数，能够分页 查询 limit 8 offset xx 跳过几行限制几行
	public static Cursor getAll(Context context) {
		ContentResolver conre = context.getContentResolver();
		return conre.query(DataChangeProvider.CONTENT_URI, new String[] { "id", "pid", "description", "price", "thumb", "specification", "amount" }, null, null, " id DESC"); // 降序查询
	}

	public static Cursor getPriceAndAmount(Context context) {
		ContentResolver conre = context.getContentResolver();
		return conre.query(DataChangeProvider.CONTENT_URI, new String[] { "price", "amount" }, null, null, " id DESC"); // 降序查询
	}

	// 算出条数
	public static int getProductsCount(Context context, Uri uri) {
		ContentResolver conre = context.getContentResolver();
		android.database.Cursor cursor = conre.query(uri, null, null, null, null); // 降序查询
		int result = cursor != null ? cursor.getCount() : 0;
		cursor.close();// 千万要关闭
		return result;

	}

	// 得到用户信息
	public static Cursor getUser(Context context,String phone ) {
		ContentResolver conre = context.getContentResolver();
		
		return conre.query(UserDataChangeProvider.CONTENT_URI, new String[] { "phone", "password", "image", "price", "username" },  "phone=?", new String[] { phone }, ""); // 降序查询
//		ContentResolver conre = context.getContentResolver();
//		return conre.query(DataChangeProvider.CONTENT_URI, new String[] { "id", "pid", "description", "price", "thumb", "specification", "amount" }, null, null, " id DESC"); // 降序查询

	}

}
